﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint.Administration;

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.Logging;

namespace SPaDevToolkit.Diagnostics
{
    public class Logger
    {
        public static void WriteLog(string message)
        {
            WriteLog(message, 0, EventSeverity.Information, null, null);
        }

        public static void WriteLog(string message, int eventId, EventSeverity severity, string area, string category)
        {
            try
            {
                ILogger logger = SharePointServiceLocator.GetCurrent().GetInstance<ILogger>();
                if(area == null || category == null)
                    logger.LogToOperations(message, eventId, severity);
                else
                    logger.LogToOperations(message, eventId, severity, area + "/" + category);
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// Writes exception to the windows event log and SharePoint log
        /// </summary>
        /// <param name="ex"></param>
        public static void WriteException(Exception ex)
        {
            WriteException(ex, 0, EventSeverity.Error, null, null);
        }

        /// <summary>
        /// Writes exception plus additional message to the windows event log and SharePoint log
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="additionalMessage"></param>
        public static void WriteException(Exception ex, string additionalMessage)
        {
            WriteException(ex, additionalMessage, 0, EventSeverity.Error, null, null);
        }

        /// <summary>
        /// Writes exception to the windows event log and SharePoint log with the specified category
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="eventId"></param>
        /// <param name="severity"></param>
        /// <param name="area"></param>
        /// <param name="category"></param>
        public static void WriteException(Exception ex, int eventId, EventSeverity severity, string area, string category)
        {
            WriteException(ex, null, eventId, severity, area, category);
        }

        /// <summary>
        /// Writes exception plus an additional message to the windows event log and SharePoint log with the specified category
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="additionalMessage"></param>
        /// <param name="eventId"></param>
        /// <param name="severity"></param>
        /// <param name="area"></param>
        /// <param name="category"></param>
        public static void WriteException(Exception ex, string additionalMessage, int eventId, EventSeverity severity, string area, string category)
        {
            try
            {
                ILogger logger = SharePointServiceLocator.GetCurrent().GetInstance<ILogger>();
                string loggerCategory = (area == null || category == null ? null : area + "/" + category);
                if (string.IsNullOrEmpty(additionalMessage))
                    logger.LogToOperations(ex, eventId, severity, loggerCategory);
                else
                    logger.LogToOperations(ex, additionalMessage, eventId, severity, loggerCategory);
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Writes exception to the SharePoint log
        /// </summary>
        /// <param name="ex"></param>
        public static void WriteTrace(Exception ex)
        {
            WriteTrace(ex, 0, TraceSeverity.High, null, null);
        }

        /// <summary>
        /// Write message to the SharePoint log
        /// </summary>
        /// <param name="message"></param>
        public static void WriteTrace(string message)
        {
            WriteTrace(message, 0, TraceSeverity.Verbose, null, null);
        }

        /// <summary>
        /// Writes exception plus additional message SharePoint log
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="additionalMessage"></param>
        public static void WriteTrace(Exception ex, string additionalMessage)
        {
            WriteTrace(ex, additionalMessage, 0, TraceSeverity.High, null, null);
        }

        public static void WriteTrace(Exception ex, int eventId, TraceSeverity severity, string area, string category)
        {
            WriteTrace(ex, null, eventId, severity, area, category);
        }

        public static void WriteTrace(string message, int eventId, TraceSeverity severity, string area, string category)
        {
            try
            {
                ILogger logger = SharePointServiceLocator.GetCurrent().GetInstance<ILogger>();
                string loggerCategory = (area == null || category == null ? null : area + "/" + category);
                logger.TraceToDeveloper(message, eventId, severity, loggerCategory);
            }
            catch (Exception)
            {
            }
        }

        public static void WriteTrace(Exception ex, string message, int eventId, TraceSeverity severity, string area, string category)
        {
            try
            {
                ILogger logger = SharePointServiceLocator.GetCurrent().GetInstance<ILogger>();
                string loggerCategory = (area == null || category == null ? null : area + "/" + category);
                if (string.IsNullOrEmpty(message))
                    logger.TraceToDeveloper(ex, eventId, severity, loggerCategory);
                else
                    logger.TraceToDeveloper(ex, message, eventId, severity, loggerCategory);
            }
            catch (Exception)
            {
            }
        }
    }
}
